<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/*class Tree {

    //传递一个父级ID返回所有子集的LIST
    Static Public function getChilds($cate,$pid){
        $arr = array();
        foreach($cate as $v){
            if($v['pid'] == $pid){
                $arr[] = $v;
                $arr = array_merge($arr,self::getChildsId($cate,$v['id']));
            }
        }
        return $arr;
    }

    Static Public function echoa(){
    	echo 111;
    }

}*/



//给出父节点，返回所有子节点
function getChildrensID($DataArr,$pid){

    $arr = array();
//    $NoSet = array();
    foreach($DataArr as $key => $value){
        if($value['pid'] == $pid){
            $arr[] = $value['id'];
            //echo $key;
            $arr = array_merge($arr,getChildrensID($DataArr,$value['id']));
            //echo $key;

        }
    }

    //$arr[] = 1;
    return $arr;
}

//给出父节点，返回所有填满的节点
function getFullChildrenID($DataArr,$pid){
       $arr = array();
       $temp = array();
       $FullChildrenArr = array();
       foreach($DataArr as $key => $value){
           $temp[] = $value['pid'];

       }
       $arr = array_count_values($temp);
       //dump($arr);
       foreach($arr as $key => $v){
           if($v == 2){
               //echo "等于2的";echo $key;
               $FullChildrenArr [] = $key;
           }
       }
       return $FullChildrenArr;

    }


  /**********验证码配置项**********/
  //验证码长度
  define('VERIFY_LENGTH', 4);
  //验证码图片宽度(像素)
  define('VERIFY_WIDTH', 250);
  //验证码图片高度(像素)
  define('VERIFY_HEIGHT', 60);
  //验证码背影颜色(16进制色值)
  define('VERIFY_BGCOLOR', '#F3FBFE');
  //验证码种子
  define('VERIFY_SEED', '3456789aAbBcCdDeEfFgGhHjJkKmMnNpPqQrRsStTuUvVwWxXyY');
  //验证码字体文件
  define('VERIFY_FONTFILE', 'font.ttf');
  //验证码字体大小
  define('VERIFY_SIZE', 30);
  //验证码字体颜色(16进制色值)
  define('VERIFY_COLOR', '#444444');
  //SESSION识别名称
  define('VERIFY_NAME', 'verify');
  //存储验证码到SESSION时使用函数
  define('VERIFY_FUNC', 'strtolower');


  function verify ($length = NULL, $width = NULL, $height = NULL) {

    //环境检测
    if (!self::checkCondition()) return false;

    //初始化参数(放进框架时把常量换成 读取配置文件项 )
    $length = empty($length) ? config('VERIFY_LENGTH') : $length;
    $width = empty($width) ? config('VERIFY_WIDTH') : $width;
    $height= empty($height) ? config('VERIFY_HEIGHT') : $height;
    $bgColor = config('VERIFY_BGCOLOR');
    $seed = config('VERIFY_SEED');
    $fontFile = config('VERIFY_FONTFILE');
    $size = config('VERIFY_SIZE');
    $fontColor = config('VERIFY_COLOR');
    $name = config('VERIFY_NAME');
    $fn = config('VERIFY_FUNC');

    //创建画布图像
    $verify = imagecreatetruecolor($width, $height);

    //画布背景色
    $rgb = self::colorTrans($bgColor);
    $color = imagecolorallocate($verify, $rgb['red'], $rgb['green'], $rgb['blue']);
    imagefill($verify, 0, 0, $color);

    //写入背景干扰字体
    $len = strlen($seed) - 1;
    for ($i = 0; $i < 20; $i++) {
      $color = imagecolorallocate($verify, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
      imagestring($verify, 5, mt_rand(0, $width), mt_rand(0, $height), $seed[mt_rand(0, $len)], $color);
    }

    $rgb = self::colorTrans($fontColor);
    $fontColor = imagecolorallocate($verify, $rgb['red'], $rgb['green'], $rgb['blue']);


    //写入验证码
    $code = '';
    $left = ($width - $size * $length) / 2;
    $y = $height - ($height - $size) / 2;
    for ($i = 0; $i < $length; $i++) {
      $font = $seed[mt_rand(0, $len)];
      $x = $size * $i + $left;
      imagettftext($verify, $size, mt_rand(-30, 30), $x, $y, $fontColor, $fontFile, $font);
      $code .= $font;
    }
    $_SESSION[$name] = $fn($code);

    //干扰线
    for ($i = 0, $h = $height / 2 - 2; $i < 5; $i++, $h++) {
      $color = imagecolorallocate($verify, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
      $cx = mt_rand(-10, $width + 10);
      $cy = mt_rand(-10, $height + 10);
      $cw = mt_rand(-10, $width + 10);
      $ch = mt_rand(-10, $height + 10);
      imageellipse($verify, $cx, $cy, $cw, $ch, $color);
      imageellipse($verify, $cx + 1, $cy + 1, $cw + 1, $ch + 1, $color);

      imageline($verify, 0, $h, $width, $h, $fontColor);
    }

    //输入图像
    header('Content-type:image/png');
    imagepng($verify);
    imagedestroy($verify);
    return true;
  }


